package com.mgface.alg.二叉树;

/**
 * @author wanyuxiang
 * @version 1.0
 * @project Java_Alg
 * @create 2019-11-05 11:05
 **/
public class Test {
    public static class BiTNode{
        int data;
        BiTNode lchild,rchild;
    }
    public static BiTNode arraytotree(int []arr,int start,int end){
        BiTNode root = null;
        if (end>=start){
            root =new  BiTNode();
            int mid = (start+end)/2;
            root.data = arr[mid];
            root.lchild = arraytotree(arr,start,mid-1);
            root.rchild = arraytotree(arr,mid+1,end);
        }else {
            root = null;
        }
        return root;
    }
    public static void printTreeMidOrder(BiTNode root){
        if (root == null) return;
        if (root.lchild!=null){
            printTreeMidOrder(root.lchild);
        }
        System.out.print(root.data+" ");
        if (root.rchild!=null){
            printTreeMidOrder(root.rchild);
        }
    }

    public static void main(String[] args) {
        int arr[] = {1,2,3,4,5,6,7,8,9,10};
        System.out.println("数组:");
        for (int value : arr) {
            System.out.print(value + " ");
        }
        System.out.println();
        BiTNode root = arraytotree(arr,0,arr.length-1);
        System.out.println("中序遍历:");
        printTreeMidOrder(root);
    }
}
